package cn.johnyu.demo.util;

import java.sql.*;

public class Dbutil {
    private static final String url = "jdbc:mysql://localhost:3306/test";
    private static final String username = "root";
    private static final String password="123";
    private static final String driver="com.mysql.cj.jdbc.Driver";

    public static Connection getConnection()  {
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void close(Connection conn, Statement statement, ResultSet rs)  {
        try {
            if(rs!=null){
                rs.close();
            }
            if(statement!=null){
                statement.close();
            }
            if(conn!=null){
                conn.close();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
